<?php

class Backend_LoginController extends Zend_Controller_Action {
    
    private $sessionLogin;

    /**
     * Constructor
     */
    public function init() {
        $this->_helper->layout->setLayout('login');
        $this->sessionLogin = new Zend_Session_Namespace('Admin');
        if(!empty($this->sessionLogin->admininfo)) {
            $this->_redirect('backend');
        }
    }

    /**
     * Main Back End
     */
    public function indexAction() {
        // Set title layout
        $this->view->headTitle('Login | Backend');

        if ($this->_request->isPost()) {
            $arrParams = $this->_request->getParams();
            $result = Service_Model::factory('AdminInfo', 'getAdminInfo', $arrParams, 'backend');
            if (!empty($result)) {
                if ($result['password'] == md5($arrParams['password'])) {
                    $this->sessionLogin = new Zend_Session_Namespace('Admin');
                    $this->sessionLogin->admininfo = array(
                        'userid'  => $result['userid'],
                        'username' => $result['username'],
                        'email'     => $result['email']
                    );

                    $this->_redirect('/backend');
                    exit();
                }
                else {
                    $errorFlag = true;
                }
            }
            else {
                $errorFlag = true;
            }
            if ($errorFlag) {
                $this->view->error = 'Email or password incorrectly!';
            }
        }
        //End If
    }

    
}

